<template>
  <!-- 下一个执行人弹出层 -->
    <a-form-model>
      <a-form-model-item v-for="(item, index) in variableList" :key="index" :label="item.name">
        <template v-if="item.candidataGroups">
          <candidataGroups
            :applyid="$route.params.applyid"
            :groupList="item.candidataGroups"
            :userList="item.candidataUsers"
            :variable="item.variable"
            @f_setVariable="s_setVariable"
          />
        </template>
        <template v-else>
          <candidataAssignee :variable="item.variable" @f_setVariable="s_setVariable" />
        </template>
      </a-form-model-item>
    </a-form-model>
</template>
<script>
export default {
  props: {
    f_nextList: {
      typeof: Array,
      require: true,
    },
  },
  data() {
    return {
      variableMap: {}, // 流程变量
      variableList: [], // 有变量的环节
    }
  },
  created() {
    this.init()
  },
  methods: {
    saveRecord() {
      let result = this.check()
      if (false == result.status) {
        this.$message.warning(result.msg)
        return
      }
      this.$emit('f_setTaskVariable', result.data)
    },
    // 校验所有节点是否都配置了人员
    check() {
      let result = { status: false, msg: '', data: this.variableMap }
      for (let key in this.variableMap) {
        if (this.variableMap[key] == null) {
          for (let v of this.variableList) {
            if (v.variable == key) {
              result.msg = `请选择[${v.name}]的处理人`
              break
            }
          }
          return result
        }
      }
      result.status = true
      return result
    },
    // 子组件候选变量赋值
    s_setVariable(variable, value) {
      console.log('子组件候选', variable, value)
      this.variableMap[variable] = value
      this.saveRecord()
    },
    init() {
      this.variableList = []
      // 去掉表达式 ${} 符号
      this.f_nextList.forEach((v) => {
        v.assignee.forEach((assignee) => {
          if (assignee.indexOf('$') > -1) {
            let variable = assignee.slice(assignee.indexOf('${') + 2, assignee.indexOf('}'))
            this.variableMap[variable] = null
            v.variable = variable
            this.variableList.push(v)
          }
        })
      })
      console.log('弹框下一步', this.variableList, this.$route.params.applyid)
    },
  },
  components: {
    deptUserTree: () => import('@/views/base/DeptUserTree.vue'),
    candidataGroups: () => import('@/views/workflow/Wf_candidataGroups.vue'),
    candidataAssignee: () => import('@/views/workflow/Wf_candidataAssignee.vue'),
  },
}
</script>

<style lang="less" scoped>
</style>
